export default {
    // cover裁剪图片
    drawCoverImage(url, ctx, startX, startY, cwidth, cheight) {
        return new Promise(resolve => {
            exec()

            async function exec() {
                wx.getImageInfo({
                    src: url,
                    success: function (res) {
                        let sw, sh, imageRatio, canvasRatio
                        canvasRatio = cwidth / cheight
                        const {width, height} = res
                        imageRatio = width / height
                        if (imageRatio <= canvasRatio) {
                            sw = width
                            sh = sw / canvasRatio
                            startX = 0
                            startY = (height - sh) / 2
                        } else {
                            sh = height
                            sw = sh * canvasRatio
                            startX = (width - sw) / 2
                            startY = 0
                        }
                        const filePath = res.path
                        ctx.drawImage(filePath, startX, startY, sw, sh, 0, 0, cwidth, cheight)
                        resolve()
                    }
                })
            }
        })
    }
}